﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PropertyChangedEvent
{
    public class Person : IPropertyChangeNotification
    {
        private string firstName = null;
        private string lastName = null;
        private string gender = null;
        private string birthDate = null;

        public string FirstName
        {
            get { return this.firstName; }
            set
            {
                if (value != null && this.firstName != value)
                {
                    OnPropertyChanged("First Name", this.firstName, value);
                    this.firstName = value;
                }
            }
        }
        public string LastName
        {
            get { return this.lastName; }
            set
            {
                if (value != null && this.lastName != value)
                {
                    OnPropertyChanged("Last Name", this.lastName, value);
                    this.lastName = value;
                }
            }
        }
        public string Gender
        {
            get { return this.gender; }
            set
            {
                if (value != null && this.gender != value)
                {
                    OnPropertyChanged("Gender", this.gender, value);
                    this.gender = value;
                }
            }
        }
        public string BirthDate
        {
            get { return this.birthDate; }
            set
            {
                if (value != null && this.birthDate != value)
                {
                    OnPropertyChanged("Birth date", this.birthDate, value);
                    this.birthDate = value;
                }
            }
        }

        public Person(string firstName, string lastName, string gender, string birthDate)
        {
            this.firstName = firstName;
            this.lastName = lastName;
            this.gender = gender;
            this.birthDate = birthDate;
            this.PropertyChangedEventHandler = new PropertyChangedEventHandler(
                delegate(object sender, PropertyChangedEventArgs eventArgs)
                {
                    Console.WriteLine("{0} property was changed from {1} to {2}", 
                        eventArgs.Name, eventArgs.OldValue, eventArgs.NewValue);
                }
                );
        }

        protected void OnPropertyChanged(string propertyName, object oldValue, object newValue)
        {
            if (PropertyChangedEventHandler != null)
            {
                PropertyChangedEventHandler(this, new PropertyChangedEventArgs(propertyName, oldValue, newValue));
            }
        }

        public event PropertyChangedEventHandler PropertyChangedEventHandler;
    }
}
